<?php
/**
 * autoBeat actions.
 *
 * @package    ##PROJECT_NAME##
 * @subpackage autoBeat
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: actions.class.php 9855 2008-06-25 11:26:01Z FabianLange $
 */
class idolActions extends sfActions
{
	public function executeIndex()
	{
		return $this->forward('idol', 'list');
	}

	public function executeList()
	{
			
		//hungtd test
			
			
		//echo 'count='.iringback::countFiles('/iring_storage/ringtone/beat/');

			
		$this->processSort();

		$this->processFilters();

		$this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/beat/filters');

		// pager
		$this->pager = new sfPropelPager('Beat', 20);
		$c = new Criteria();
		$this->addSortCriteria($c);
		$this->addFiltersCriteria($c);
		$this->pager->setCriteria($c);
		$this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/beat')));
		$this->pager->init();
		// save page
		if ($this->getRequestParameter('page')) {
			$this->getUser()->setAttribute('page', $this->getRequestParameter('page'), 'sf_admin/beat');
		}
	}
	public function executeCreate()
	{
		return $this->forward('beat', 'edit');
	}

	public function executeSave()
	{
		return $this->forward('beat', 'edit');
	}

	protected function processFilters()
	{
		if ($this->getRequest()->hasParameter('filter'))
		{
			$filters = $this->getRequestParameter('filters');

			$this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/beat');
			$this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/beat/filters');
			$this->getUser()->getAttributeHolder()->add($filters, 'sf_admin/beat/filters');
		}
	}

	protected function processSort()
	{
		if ($this->getRequestParameter('sort'))
		{
			$this->getUser()->setAttribute('sort', $this->getRequestParameter('sort'), 'sf_admin/beat/sort');
			$this->getUser()->setAttribute('type', $this->getRequestParameter('type', 'asc'), 'sf_admin/beat/sort');
		}

		if (!$this->getUser()->getAttribute('sort', null, 'sf_admin/beat/sort'))
		{
		}
	}

	protected function addFiltersCriteria($c)
	{
		if (isset($this->filters['id_is_empty']))
		{
			$criterion = $c->getNewCriterion(BeatPeer::ID, '');
			$criterion->addOr($c->getNewCriterion(BeatPeer::ID, null, Criteria::ISNULL));
			$c->add($criterion);
		}
		else if (isset($this->filters['id']) && $this->filters['id'] !== '')
		{
			$c->add(BeatPeer::ID, $this->filters['id']);
		}
		if (isset($this->filters['beat_name_is_empty']))
		{
			$criterion = $c->getNewCriterion(BeatPeer::BEAT_NAME, '');
			$criterion->addOr($c->getNewCriterion(BeatPeer::BEAT_NAME, null, Criteria::ISNULL));
			$c->add($criterion);
		}
		else if (isset($this->filters['beat_name']) && $this->filters['beat_name'] !== '')
		{
			$c->add(BeatPeer::BEAT_NAME, '%' .strtr($this->filters['beat_name'], '*', '%').'%', Criteria::LIKE);
		}
		if (isset($this->filters['artist_is_empty']))
		{
			$criterion = $c->getNewCriterion(BeatPeer::ARTIST, '');
			$criterion->addOr($c->getNewCriterion(BeatPeer::ARTIST, null, Criteria::ISNULL));
			$c->add($criterion);
		}
		else if (isset($this->filters['artist']) && $this->filters['artist'] !== '')
		{
			$c->add(BeatPeer::ARTIST, strtr($this->filters['artist'], '*', '%'), Criteria::LIKE);
		}
		if (isset($this->filters['composer_is_empty']))
		{
			$criterion = $c->getNewCriterion(BeatPeer::COMPOSER, '');
			$criterion->addOr($c->getNewCriterion(BeatPeer::COMPOSER, null, Criteria::ISNULL));
			$c->add($criterion);
		}
		else if (isset($this->filters['composer']) && $this->filters['composer'] !== '')
		{
			$c->add(BeatPeer::COMPOSER, strtr($this->filters['composer'], '*', '%'), Criteria::LIKE);
		}
		if (isset($this->filters['album_is_empty']))
		{
			$criterion = $c->getNewCriterion(BeatPeer::ALBUM, '');
			$criterion->addOr($c->getNewCriterion(BeatPeer::ALBUM, null, Criteria::ISNULL));
			$c->add($criterion);
		}
		else if (isset($this->filters['album']) && $this->filters['album'] !== '')
		{
			$c->add(BeatPeer::ALBUM, strtr($this->filters['album'], '*', '%'), Criteria::LIKE);
		}
		if (isset($this->filters['track_no_is_empty']))
		{
			$criterion = $c->getNewCriterion(BeatPeer::TRACK_NO, '');
			$criterion->addOr($c->getNewCriterion(BeatPeer::TRACK_NO, null, Criteria::ISNULL));
			$c->add($criterion);
		}
		else if (isset($this->filters['track_no']) && $this->filters['track_no'] !== '')
		{
			$c->add(BeatPeer::TRACK_NO, $this->filters['track_no']);
		}
		if (isset($this->filters['genre_is_empty']))
		{
			$criterion = $c->getNewCriterion(BeatPeer::GENRE, '');
			$criterion->addOr($c->getNewCriterion(BeatPeer::GENRE, null, Criteria::ISNULL));
			$c->add($criterion);
		}
		else if (isset($this->filters['genre']) && $this->filters['genre'] !== '')
		{
			$c->add(BeatPeer::GENRE, strtr($this->filters['genre'], '*', '%'), Criteria::LIKE);
		}

		//HungInDay them code
		if (isset($this->filters['genre_id_is_empty']))
		{
			$criterion = $c->getNewCriterion(BeatPeer::GENRE_ID, '');
			$criterion->addOr($c->getNewCriterion(BeatPeer::GENRE_ID, null, Criteria::ISNULL));
			$c->add($criterion);
		}
		else if (isset($this->filters['genre_id']) && $this->filters['genre_id'] !== '')
		{
			$c->add(BeatPeer::GENRE_ID, strtr($this->filters['genre_id'], '*', '%'), Criteria::LIKE);
		}

		if (isset($this->filters['beat_format_is_empty']))
		{
			$criterion = $c->getNewCriterion(BeatPeer::BEAT_FORMAT, '');
			$criterion->addOr($c->getNewCriterion(BeatPeer::BEAT_FORMAT, null, Criteria::ISNULL));
			$c->add($criterion);
		}
		else if (isset($this->filters['beat_format']) && $this->filters['beat_format'] !== '')
		{
			$c->add(BeatPeer::BEAT_FORMAT, strtr($this->filters['beat_format'], '*', '%'), Criteria::LIKE);
		}
		if (isset($this->filters['record_is_empty']))
		{
			$criterion = $c->getNewCriterion(BeatPeer::RECORD, '');
			$criterion->addOr($c->getNewCriterion(BeatPeer::RECORD, null, Criteria::ISNULL));
			$c->add($criterion);
		}
		else if (isset($this->filters['record']) && $this->filters['record'] !== '')
		{
			$c->add(BeatPeer::RECORD, $this->filters['record']);
		}
	}

	protected function addSortCriteria($c)
	{
		if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/beat/sort'))
		{
			$sort_column = BeatPeer::translateFieldName($sort_column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
			if ($this->getUser()->getAttribute('type', null, 'sf_admin/beat/sort') == 'asc')
			{
				$c->addAscendingOrderByColumn($sort_column);
			}
			else
			{
				$c->addDescendingOrderByColumn($sort_column);
			}
		}
	}

	protected function getLabels()
	{
		return array(
      'beat{id}' => 'Id:',
      'beat{beat_name}' => 'Tên file:',
      'beat{karaoke_file_path}' => 'File karaoke:',
      'beat{lyric_file_path}' => 'File lời:',
      'beat{artist}' => 'Ca sĩ:',
      'beat{composer}' => 'Tác giả:',
      'beat{album}' => 'Album:',
      'beat{track_no}' => 'Track no:',
      'beat{genre}' => 'Thể loại:',
      'beat{lyrics}' => 'Lời nhạc:',
      'beat{bitrate}' => 'Bitrate:',
      'beat{duration}' => 'Duration:',
      'beat{beat_format}' => 'Định dạng:',
      'beat{record}' => 'Số lần thu:',
		);
	}
}
